package kr.hkit.jenny.coffee.service;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;

import kr.hkit.jenny.coffee.dao.CoffeeDao;
import kr.hkit.jenny.coffee.dto.SalesRecord;
import java.awt.GridLayout;

public class ListDlg extends JDialog implements ActionListener {

	private JTable table;
	private CoffeeDao dao;
	private ArrayList<SalesRecord> lists;
	private String[][] arLists;

	private String[] colNames = { "제품코드", "제품명", "제품단가", "판매수량", "마진율", "판매금액",
			"부가세액", "공급가액", "마진액" };

	private final JPanel contentPanel = new JPanel();
	private JButton okButton;
	private boolean orderBySelling;
	public static final int[] FIELD_SIZES = { 100, 250, 150, 150, 150, 150, 150, 150, 150 };

	/**
	 * Create the dialog.
	 */
	public ListDlg(CoffeeDao dao, boolean orderBySelling) {
		
		this.dao = dao;
		this.orderBySelling = orderBySelling;
		
		setBounds(100, 100, 649, 200);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new GridLayout(0, 1, 0, 0));
		{
			JScrollPane scrollPane = new JScrollPane();
			contentPanel.add(scrollPane);
			table = new JTable(new String[0][0], colNames);
			table.setAutoCreateRowSorter(true);
			scrollPane.setViewportView(table);
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				okButton = new JButton("OK");
				okButton.addActionListener(this);
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
		}
		loadData();
		tableCellAlginAndWidth();
	}

	public void loadData() {
		try {
			lists = dao.listItems();
			sorting();
			int len = lists.size();
			arLists = new String[len][colNames.length];
			for (int i = 0; i < len; i++) {
				SalesRecord temp = lists.get(i);
				arLists[i] = temp.toArray();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			TableModel model = new DefaultTableModel(arLists, colNames);
			table.setModel(model);
		}
	}

	private void sorting() {
		if (orderBySelling) {
			setTitle("판매 금액 정렬");
			sortItemList(new Comparator<SalesRecord>() {

				@Override
				public int compare(SalesRecord o1, SalesRecord o2) {
					return o1.getSellingPrice() - o2.getSellingPrice();
				}
			});
		} else {
			setTitle("마진액 정렬");
			sortItemList(new Comparator<SalesRecord>() {

				@Override
				public int compare(SalesRecord o1, SalesRecord o2) {
					return o1.getMargin() - o2.getMargin();
				}
			});
		}
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == okButton) {
			actionPerformedOkButton(e);
		}
	}

	protected void actionPerformedOkButton(ActionEvent e) {
		setVisible(false);
	}

	private void sortItemList(Comparator<SalesRecord> comparator) {
		Collections.sort(lists, comparator);
	}
	
	private void tableCellAlginAndWidth() {
		DefaultTableCellRenderer dtcrCenter = new DefaultTableCellRenderer();
		dtcrCenter.setHorizontalAlignment(SwingConstants.CENTER);
				
		TableColumnModel cModel = table.getColumnModel();
		
		for(int i=0; i<colNames.length; i++){
			cModel.getColumn(i).setPreferredWidth(FIELD_SIZES[i]);
			cModel.getColumn(i).setCellRenderer(dtcrCenter);
		}
	}

}
